package com.example.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.entity.RolesEntity;
import com.example.entity.UserrolesEntity;
import com.example.entity.UsersEntity;
import com.example.entity.UsersVoEntity;
import com.example.service.UsersService;

@RequestMapping("/user")
@RestController
public class UsersController {
	
	@Autowired
	private UsersService usersService;
	
	/**
	 * 多条件查询
	 * @param userVO
	 * @return
	 */
	@RequestMapping("/GetUsersLayUI")
	public Map<String, Object> GetUsersLayUI(UsersVoEntity usersVoEntity){
		Map<String, Object> reMap=new HashMap<String, Object>();
		usersVoEntity.setOffset((usersVoEntity.getPage()-1)*usersVoEntity.getLimit());
		List<UsersEntity> list=usersService.GetUsersLayUI(usersVoEntity);
		int count=usersService.getCount(usersVoEntity);
		reMap.put("code","0");
		reMap.put("msg", "success");
		reMap.put("count", count);
		reMap.put("data", list);
		return reMap;
	}
	
	/**
	 * 新增用户
	 * @param id
	 * @return
	 */

	@RequestMapping(path = "/addUser")
	public Map<String, Object> addUser(UsersEntity usersEntity) { // 受影响的行数 
		int rows = usersService.addUser(usersEntity);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "添加成功" : "添加失败");
		return rsMap;

	}
	
	/**
	 * 修改用户
	 * @param id
	 * @return
	 */

	@RequestMapping(path = "/upUser")
	public Map<String, Object> upUser(UsersEntity usersEntity) { // 受影响的行数 
		int rows = usersService.upUser(usersEntity);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "修改成功" : "修改失败");
		return rsMap;

	}
	
	/**
	 * 根据id删除用户
	 * @param id
	 * @return
	 */
	@PostMapping("/delUser")
	@ResponseBody
	public Map<String, Object> delUser(int id){
		Map<String, Object> relMap=new HashMap<String, Object>();
		int rows=usersService.delUser(id);
		relMap.put("code",rows>0? "0":"1");
		relMap.put("msg",rows>0? "删除成功":"删除失败");
		return relMap;
	}
	/**
	 * 锁定
	 * @param id
	 * @return
	 */

	@PostMapping(path = "/isLock")
	public Map<String, Object> isLock(int id) { // 受影响的行数 
		int rows = usersService.islock(id);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "锁定成功" : "锁定失败");
		return rsMap;

	}
	/**
	 * 解锁
	 * @param id
	 * @return
	 */

	@PostMapping(path = "/noIsLock")
	public Map<String, Object> noIsLock(int id) { // 受影响的行数 
		int rows = usersService.noIslock(id);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "解锁成功" : "解锁失败");
		return rsMap;

	}
	
	/**
	 * 重置密码
	 * @param id
	 * @return
	 */
	@PostMapping("/ResetPassword")
	@ResponseBody
	public Map<String, Object> ResetPassword(int id){
		Map<String, Object> relMap=new HashMap<String, Object>();
		int rows=usersService.ResetPassword(id);
		relMap.put("code",rows>0? "0":"1");
		relMap.put("msg",rows>0? "重置成功":"重置失败");
		return relMap;
	}
	/**
	 *根据用户id获取角色
	 * @param id
	 * @return
	 */
	@PostMapping(path = "/getRolesByUid")
	public Map<String, Object> getRolesByUid(Integer id) { // 受影响的行数
		
		List<RolesEntity> list = usersService.getRolesByUid(id);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", "code");
		rsMap.put("msg","");
		rsMap.put("count", list.size());
		rsMap.put("data",list);
		return rsMap;

	}
	/**
	 * 查询所有角色
	 * @param id
	 * @return
	 */
	@PostMapping(path = "/GetRolesAll")
	public Map<String, Object> GetRolesAll() { // 受影响的行数
		
		List<RolesEntity> list = usersService.GetRolesAll();
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", "code");
		rsMap.put("msg","");
		rsMap.put("count", list.size());
		rsMap.put("data",list);
		return rsMap;

	}
	/**
	 * 添加用户身份
	 * @param ur
	 * @return
	 */
	@RequestMapping("/addUR")
	@ResponseBody
	public Map<String, Object> addUR(UserrolesEntity ur) {
		int rows = usersService.addUR(ur);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "添加成功" : "添加失败");
		return rsMap;

	}
	/**
	 * 删除用户身份
	 * @param ur
	 * @return
	 */
	@RequestMapping("/delUR")
	@ResponseBody
	public Map<String, Object> delUR(UserrolesEntity ur) {
		int rows = usersService.delUR(ur);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "删除成功" : "删除失败");
		return rsMap;

	}
}
